package br.fbc.codigodebarras;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class PegarInfos {

	private static final String FORMAT = "png";
	private static final int WHITE = 0xFFFFFFFF;
	private static final int BLACK = 0xFF000000;
	private static final int RED = 0xFFFF0000;
	private static boolean rowSampling = false;

	private File file;

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		File file = new File("C:\\temp\\qrfile.jpg");
		PegarInfos pi = new PegarInfos(file);

		System.out.println(pi.getDecodedText());

	}

	public PegarInfos(File file) {
		this.file = file;
	}

	public String getDecodedText() {
		BufferedImage image;
		try {
			image = ImageIO.read(file);
		} catch (IOException ioe) {
			return ioe.toString();
		}
		if (image == null) {
			return "Could not decode image";
		}
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Result result;
		try {
			result = new MultiFormatReader().decode(bitmap);
		} catch (ReaderException re) {
			return re.toString();
		}
		return result.getText();
	}

}
